/**
 * Created by jiangzhiyong on 15/10/14.
 */

var Manage = {
    init : function() {
        Manage.get_data();
        $("#data_table tbody").find("a[name='update_brand']").click(Manage.brand_show_update);
        $("a[name='add_brand']").click(Manage.brand_show_add);
        $("#data_table tbody").find("a[name='delete_brand']").click(function() {
        		var td = $(this).parent();
    			var brand_id = $(td).attr("brand_id");
    			if (confirm("确定要删除该品牌?")) {
    				//编辑
        			ajax("/brands/remove", {"brand_id": brand_id}, success);
    			}
        });
        $("#do_update_brand").click(function(){
        		var brand_id = $("#update_brand").find("input[name='brand_id']").val();
        		var brand_name = $("#update_brand").find("input[name='brand_name']").val();
        		var brand_wiki = $("#update_brand").find("textarea[name='brand_wiki']").val();
        		if (brand_name == null || brand_name == "") {
        			alert("科目名称不能为空");
        			return false;
        		}
        		if(brand_id == "" || brand_id == 0) {
        			//新增
        			ajax("/brands/create", {"name" : brand_name, "wiki" : brand_wiki}, success);
        		} else {
        			//新增
        			ajax("/brands/update", {"brand_id" : brand_id, "name" : brand_name, "wiki" : brand_wiki}, success);
        		}
        });
    },
    get_data : function() {
        var data = {};
        ajax("/brands/list", data, callback);
    },
    brand_show_add : function() {
    		$("#update_brand").find("input[name='brand_id']").val(0);
    		$("#update_brand").find("input[name='brand_name']").val("");
    		$("#update_brand").find("textarea[name='brand_wiki']").val("");
    		$("#update_brand").modal('show');
    },
    brand_show_update : function() {
    		var td = $(this).parent();
    		var brand_id = $(td).attr("brand_id");
    		$("#update_brand").find("input[name='brand_id']").val(brand_id);
    		var brand_name = $(td).prev().prev().text();
    		$("#update_brand").find("input[name='brand_name']").val(brand_name);
    		var brand_wiki = $(td).prev().text();
    		$("#update_brand").find("textarea[name='brand_wiki']").val(brand_wiki);
    		$("#update_brand").modal('show');
    },
    init_table : function(result) {
    		if (result != null && result.data != null && result.data.brands.length > 0) {
    			var tbody = $("#data_table tbody");
    			tbody.empty();
    			var brands = result.data.brands;
    			var trs = "";
    			for (var i=0; i < brands.length; i++) {
    				var brandOne = brands[i];
    				trs += ("<tr>");
    				var brandName = ("<td>" + brandOne.name +"</td>");
    				var brandWiki = ("<td>" + (brandOne.wiki == undefined ? "" : brandOne.wiki) +"</td>");
    				var operate = ("<td brand_id='" + brandOne.id + "'><a name='update_brand' href='javascript:void(0)'>更新</a> &emsp; <a name='delete_brand' href='javascript:void(0)'>删除</a></td>");
    				trs += (brandName + brandWiki + operate);
    				trs += ("</tr>");
    			}
    			if (brands.length == 0) {
				trs += ("<tr>");
				trs += ("<td colspan='3'>未查询到任何结果</td>");
				trs += ("</tr>");
			}
    			tbody.append(trs);
    			
    		}
    }
};

function callback(result) {
	Manage.init_table(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function(){
    Manage.init();
})
